package com.bclz.bio;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ClientBIO implements Runnable {

    private int port;

    public ClientBIO(int port) {
        this.port = port;
    }


    public void run() {
        Socket socket = null;
        try {
            socket = new Socket("localhost", port);
//            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

            for (int i = 0; i < 10; i++) {
                //发送带有\s、\n回车或者换行
                if (i == 9) {
                    out.println("end");
                } else {

                    out.println("   "+i + "Message-"+Thread.currentThread().getName()+"    " );

                }

//                System.out.println("Client"+Thread.currentThread().getName()+"  "+in.readLine());
            }

        } catch (IOException e) {
            System.out.println("e = " + e);
        } finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    System.out.println("e = " + e);
                }
            }
        }

    }

    public static void main(String[] args) {



            ExecutorService service = Executors.newCachedThreadPool();
            service.execute(new Thread(new ClientBIO(1919),"Client1"));
            service.execute(new Thread(new ClientBIO(1919),"Client2"));

            //关闭线程池不接受其它任务
            service.shutdown();
            //等待所有任务完成
//            service.awaitTermination(Long.MAX_VALUE,TimeUnit.MINUTES);

    }
}
